﻿@using Blazorise.Docs.Models.ApiDocsDtos
<DocsPageSubtitle>
    API
</DocsPageSubtitle>

@if ( hasComponentTypes )
{
    <ComponentApiDocsProperty ApiDocsForComponents="apiDocsForComponents"
                              MultipleComponentTypes="multipleComponentTypes"
                              PropertyType="ComponentApiDocsProperty.PropertyTypes.Parameters" />

    <ComponentApiDocsProperty ApiDocsForComponents="apiDocsForComponents"
                              MultipleComponentTypes="multipleComponentTypes"
                              PropertyType="ComponentApiDocsProperty.PropertyTypes.Events" />
}

@if ( apiDocsForComponentsMethods.Any() )
{
    <Heading Margin="Margin.Is2.FromTop.Is3.FromBottom" Size="HeadingSize.Is3">
        Methods
    </Heading>

    @foreach ( var apiDocsForComponent in apiDocsForComponentsMethods )
    {
        <DocsMethods Name="Method" Title="@(multipleComponentTypes ? apiDocsForComponent.TypeName : null)">
            @foreach ( var method in apiDocsForComponent.Methods )
            {
                <DocsMethodsItem Name="@method.Name" ReturnType="@method.ReturnTypeName"
                                 Parameters="@(string.Join(", ", method.Parameters.Select(x => $"{x.TypeName} {x.Name}")))">
                    @((MarkupString)method.Summary)
                    <ComponentApiDocsRemarks Value="@method.Remarks" />
                </DocsMethodsItem>
            }
        </DocsMethods>
    }
}
@code {
    [Parameter] public List<Type> ComponentTypes { get; set; } = [];

    /// <summary>
    /// Defines the name of the category, or a folder within the Blazorise source that will be part of the search critera.
    /// Must be used with the <see cref="ComponentsApiDocsGenerator.Categories"/> property.
    /// </summary>
    [Parameter] public string Category { get; set; }

    /// <summary>
    /// Defines the name of the subcategory, or a sub folder within the Blazorise source that will be part of the search critera.
    /// </summary>
    [Parameter] public string Subcategory { get; set; }
    
    /// <summary>
    ///  Defines the specific component to be used
    /// </summary>
    [Parameter] public ApiDocsForComponent ApiDocsForComponent { get; set; }

    IReadOnlyList<ApiDocsForComponent> apiDocsForComponents;
    List<ApiDocsForComponent> apiDocsForComponentsMethods;
    bool multipleComponentTypes;
    bool hasComponentTypes;

    protected override void OnInitialized()
    {
        apiDocsForComponents = ApiDocsForComponent is not null
        ? [ApiDocsForComponent]
        : Category is not null
            ? ComponentsApiDocsSource.Instance.Components.Where( x => x.Value.Category == Category && x.Value.Subcategory == Subcategory ).Select( x => x.Value ).ToList()
            : ComponentTypes.Select( x => ComponentsApiDocsSource.Instance.Components.GetValueOrDefault( x ) ).Where( x => x is not null ).ToList();
        hasComponentTypes = apiDocsForComponents.Any();
        multipleComponentTypes = apiDocsForComponents.Count > 1;
        apiDocsForComponentsMethods = apiDocsForComponents.Where( x => x.Methods.Any() ).ToList();
    }
}